#include "Logger.hpp"
#include "MySqlClient.hpp"
#include "Util.hpp"
#include <iostream>
#include <vector>
#include "Database.hpp"
#include "OnlineManager.hpp"
#include "Room.hpp"
#include "Logger.hpp"
#include "Session.hpp"
#include "Matcher.hpp"
#include "Server.hpp"

static const std::string host = "localhost";
static const std::string user = "online_Gomoku";
static const std::string passwd = "88482bwasdAAA@";
static const std::string database = "online_Gomoku";

void TestLogger()
{
    DLOG("%s-%d", "hello", 100);
    printf("123" "456\n");
    std::cout << "123" "456" << std::endl;
}

void TestMySql()
{
    MySqlClient mysql("localhost", "online_Gomoku", "88482bwasdAAA@", "online_Gomoku");
    auto ret = mysql.Query("SELECT * FROM user");
    auto res = ret.second;
    int m = res->RowNum(), n = res->FieldNum();
    MYSQL_ROW row;
    MYSQL_FIELD* field;
    while(field = res->NextField())
    {
        std::cout << field->name << "\t";
    }
    std::cout << std::endl;
    while(row = res->NextRow())
    {
        for(int i = 0; i < n; i++)
        {
            std::cout << row[i] << "\t";
        }
        std::cout << std::endl;
    }
}

void TestJsonUtil()
{
    Json::Value root;
    root["姓名"] = "石神";
    root["年龄"] = 20;
    root["id"] = "123456";
    std::string json_str;
    Util_Json::serialize(root, json_str);
    DLOG("%s", json_str.c_str());

    Json::Value tmp;
    Util_Json::deserialize(json_str, tmp);
    DLOG("%s", tmp["姓名"].asCString());
}

void TestStringUtil()
{
    std::string str0 = "123,456";
    std::string str1 = "123,,,456,";
    std::string str2 = "123,,,456,,";
    std::string str3 = ",,123,,456,,";

    std::vector<std::vector<std::string>> v(4);
    Util_string::split(str0, ",", v[0]);
    Util_string::split(str1, ",", v[1]);
    Util_string::split(str2, ",,", v[2]);
    Util_string::split(str3, ",,", v[3]);

    for(auto& vi : v)
    {
        for(auto& str : vi)
        {
            std::cout << str << " ";
        }
        std::cout << std::endl;
    }
}

void TestFileUtil()
{
    std::string body;
    Util_file::read("Makefile", body);
    DLOG("%ld", body.size());
    DLOG("%s", body.c_str());
}

// void TestUserTable()
// {
//     Table_user tb_user("localhost", "online_Gomoku", "88482bwasdAAA@", "online_Gomoku");
//     std::string name = "liushen", passwd = "123456";
//     if(tb_user.Register(name, passwd))
//     {
//         DLOG("注册成功!");
//     }
//     if(tb_user.LoginCheck(name, passwd))
//     {
//         DLOG("登录成功!");
//     }

//     Json::Value user;
//     if(tb_user.SelectByName(name, user))
//     {
//         DLOG("SelectByName: 查找到! id=%u", user["id"].asUInt());
//     }
//     tb_user.CountWin(user["id"].asUInt());
//     if(tb_user.SelectById(user["id"].asUInt(), user))
//     {
//         DLOG("SelectById: 查找到! total_count=%u, win_count=%u", user["total_count"].asUInt(), user["win_count"].asUInt());
//     }
//     tb_user.CountLose(user["id"].asUInt());
//     if(tb_user.SelectById(user["id"].asUInt(), user))
//     {
//         DLOG("SelectById: 查找到! total_count=%u, win_count=%u", user["total_count"].asUInt(), user["win_count"].asUInt());
//     }
// }

void TestHash()
{
    if (sodium_init() < 0) {
        std::cerr << "libsodium初始化失败" << std::endl;
        return;
    }

    // 模拟用户注册：哈希密码并存储
    std::string password = "User@123456"; // 用户输入的密码
    std::string stored_hash = Util_hash::Password(password);
    std::cout << "存储的密码哈希：" << stored_hash << std::endl;

    // 模拟用户登录：验证密码
    std::string input_password = "User@123456"; // 用户登录时输入的密码
    if (Util_hash::verify(input_password, stored_hash)) {
        std::cout << "密码验证成功" << std::endl;
    } else {
        std::cout << "密码验证失败" << std::endl;
    }
}

void TestOnlineManager()
{
    OnlineManager om;
    unsigned int uid = 1;
    wsserver_t::connection_ptr ptr;
    om.EnterGameHall(uid, ptr);
    
    if(om.IsInGameHall(uid))
    {
        DLOG("Yes");
    }
    om.ExitGameHall(uid);
    if(om.IsInGameHall(uid))
    {
        DLOG("Yes");
    }
}

void TestRoom()
{
    Table_user tb(host, user, passwd, database);
    OnlineManager om;
    // Room room(1, &tb, &om);
    RoomManager rm(&tb, &om);
    RoomPtr room = rm.CreateRoom(10, 20);
}

void TestServer1()
{
    Server srv(host, user, passwd, database);
    srv.Start(8888);
}

int main()
{
    // TestMySql();
    // TestJsonUtil();
    // TestStringUtil();
    // TestFileUtil();
    // TestUserTable();
    // TestHash();
    // TestOnlineManager();
    // TestRoom();
    TestServer1();
    return 0;
}